﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.Models.JsonModels;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
using Kaiser.Modules.DC.Services;
using Kaiser.Modules.DC.Models;
using Kaiser.Models;
using System.Data;
namespace Kaiser.Modules.DC.Controllers
{
    public class DCManageController : Controller
    {
        private readonly IDCManageService _dcManageService;
        public DCManageController(IDCManageService dcManageService)
        {
            this._dcManageService = dcManageService;
        }


        #region 我要点餐模块

        /// <summary>
        /// 批量删除点餐
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelOrderDetail(string ids)
        {

            JsonReturnMessages result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {

                result.success = _dcManageService.DeleteOrderDetailList(ids) > 0;

                if (result.success)
                {
                    result.msg = "删除成功";

                }
                else
                {
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

         /// <summary>
         /// 编辑点餐信息
         /// </summary>
         /// <param name="id"></param>
         /// <param name="model"></param>
         /// <param name="form"></param>
         /// <returns></returns>
        [HttpPost]
        public ActionResult EditOrderDetail(string id, OrderDetailInput model, FormCollection form)
        {
            JsonReturnMessages result = new JsonReturnMessages();

            try
            {

                if (id.IsNullOrWhiteSpace())
                {
                    model.OrderCreateDateTime = DateTime.Now;
                    model.OrderProductIsHot =Convert.ToBoolean(form["OrderProductIsHot"]);
                    _dcManageService.InsertOrderDetail(model);
                }
                else
                {
                    _dcManageService.UpdateOrderDetail(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        /// <summary>
        /// 添加点餐信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddOrderDetail(OrderDetailInput model, FormCollection form)
        {
            return EditOrderDetail("", model, form);
        }

        /// <summary>
        /// 我要点餐列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderDetailList()
        {
            return View();
        }

        /// <summary>
        /// 我要点餐列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OrderDetailList(DCSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._dcManageService.SearchOrderDetail(search, pageinfo);
            return Content(result.ToJsonString());
        }

        


        #endregion

        #region 点餐统计模块

        /// <summary>
        /// 点餐统计列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderStatisticsList()
        {
            return View();
        }


        /// <summary>
        /// 点餐统计列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OrderStatisticsList(DCSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._dcManageService.OrderStatisticsList(search, pageinfo);
            return Content(result.ToJsonString());
        }

        /// <summary>
        /// 点餐统计详细列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderDetailStatisticsList()
        {
            return View();
        }

        /// <summary>
        /// 点餐统计详细列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OrderDetailStatisticsList(DCSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._dcManageService.OrderDetailStatisticsList(search, pageinfo);
            return Content(result.ToJsonString());
        }
        
 


        #endregion

        #region 点餐发布管理模块

        
        public ActionResult OrderList()
        {
            return View();
        }

        [HttpPost]
        public ActionResult OrderList(DCSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._dcManageService.SearchOrder(search, pageinfo);
            return Content(result.ToJsonString());
        }

        public ActionResult GetOrderList(DCSearch search) 
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10;
            search.ProductName = Request["q"];
            var result = _dcManageService.SearchOrder(search, pageinfo);

            return Content(result.ToJsonString(), "text/html");
        } 

        /// <summary>
        /// 批量删除点餐发布信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
             [HttpPost]
        public ActionResult DelOrders(string ids)
        {

            var result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {
                //string id = "";
                //for (int i = 0; i < ids.Split(',').Length; i++)
                //{

                //    id += "'" + ids.Split(',')[i] + "',";
                //}
                //ids = id.Substring(0, id.Length - 1);
                result.success = _dcManageService.DeleteOrderList(ids) > 0;

                if (result.success)
                {
                    result.msg = "删除成功";

                }
                else
                {
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        /// <summary>
        /// 编辑点餐发布信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditOrder(string id, OrderInput model, FormCollection form)
        {
            var result = new JsonReturnMessages();

            try
            {

                if (id.IsNullOrWhiteSpace())
                {
                    model.OrderName = model.OrderDesc;
                    model.OrderCreateDateTime = DateTime.Now;
                    _dcManageService.InsertOrder(model);
                }
                else
                {
                    model.OrderName = model.OrderDesc;
                    model.OrderCreateDateTime = DateTime.Now;
                    _dcManageService.UpdateOrder(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        /// <summary>
        /// 添加点餐发布信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddOrder(OrderInput model, FormCollection form)
        {
            return EditOrder("", model, form);
        }

                 #endregion

        #region 产品管理


        public ActionResult DelProduct(string ids)
        {

            var result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {

                result.success = _dcManageService.DeleteProductList(ids) > 0;

                if (result.success)
                {
                    result.msg = "删除成功";

                }
                else
                {
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        [HttpPost]
        public ActionResult EditProduct(string id, ProductInput model, FormCollection form)
        {
            JsonReturnMessages result = new JsonReturnMessages();

            try
            {

                if (id.IsNullOrWhiteSpace())
                {
                    _dcManageService.InsertProduct(model);
                }
                else
                {
                    _dcManageService.UpdateProduct(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        [HttpPost]
        public ActionResult AddProduct(ProductInput model, FormCollection form)
        {
            return EditProduct("", model, form);
        }
        public ActionResult ProductList()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ProductList(DCSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = this._dcManageService.SearchProduct(search, pageinfo);
            return Content(result.ToJsonString());
        }

        [HttpPost]
        public ActionResult GetProductList(DCSearch search)
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10;
            search.ProductName = Request["q"];
            var result = _dcManageService.SearchProduct(search, pageinfo);

            return Content(result.ToJsonString(), "text/html");
        }
        #endregion

        
    }
}
